package com.marquisx.common.ui.dialog;

import android.app.AlertDialog;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.marquisx.tzdice.R;

public class NumberKeypad extends AlertDialog implements android.view.View.OnClickListener
{
	private static final String TAG = "MarquisX:NumberKeypad";
	
	public static final int MAXIMUM_DEF = 99999999;
	public static final int MINIMUM_DEF = -99999999;
	
	private int max;
	private int min;
	
	private View view;
	private final View keys[] = new View[12];
	private TextView displayView;
	private Integer result = 0;
	
	public NumberKeypad(Context context)
	{
		this(context, 0, MAXIMUM_DEF, MINIMUM_DEF);
	}
	
	public NumberKeypad(Context context, int current, int max, int min)
	{
		super(context);
		this.result = current;
		this.max = max;
		this.min = min;
		LayoutInflater factory = LayoutInflater.from(getContext());
		view = factory.inflate(R.layout.number_keypad, null);
		setView(view);
	}
	
	public void setButton(int resouceId, OnClickListener listener)
	{
		setButton(getContext().getResources().getString(resouceId), listener);
	}
	
	public void setButton2(int resouceId, OnClickListener listener)
	{
		setButton2(getContext().getResources().getString(resouceId), listener);
	}
	
	public void setButton3(int resouceId, OnClickListener listener)
	{
		setButton3(getContext().getResources().getString(resouceId), listener);
	}
	
	public void show(int current)
	{
		result = current;
		show();
	}

	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setTitle(R.string.keypad_title);
		findViews();
		setListeners();
		prepareViews();
	}
	
	private void findViews()
	{
		displayView = (TextView)view.findViewById(R.id.keypad_display);
		
		keys[0] = view.findViewById(R.id.keypad_1);
		keys[1] = view.findViewById(R.id.keypad_2);
		keys[2] = view.findViewById(R.id.keypad_3);
		keys[3] = view.findViewById(R.id.keypad_4);
		keys[4] = view.findViewById(R.id.keypad_5);
		keys[5] = view.findViewById(R.id.keypad_6);
		keys[6] = view.findViewById(R.id.keypad_7);
		keys[7] = view.findViewById(R.id.keypad_8);
		keys[8] = view.findViewById(R.id.keypad_9);
		keys[9] = view.findViewById(R.id.keypad_0);
		keys[10] = view.findViewById(R.id.keypad_plus_or_minus);
		keys[11] = view.findViewById(R.id.keypad_C);
		
	}

	private void setListeners()
	{
		for (int i = 0; i < keys.length; i++)
		{
			keys[i].setOnClickListener(this);
		}
	}
	
	private void prepareViews()
	{
		displayView.setText(result.toString());
	}
	
	@Override
	public void onClick(View v)
	{
		switch(v.getId())
		{
			case R.id.keypad_1:
			case R.id.keypad_2:
			case R.id.keypad_3:
			case R.id.keypad_4:
			case R.id.keypad_5:
			case R.id.keypad_6:
			case R.id.keypad_7:
			case R.id.keypad_8:
			case R.id.keypad_9:
			case R.id.keypad_0:
				showResult(Integer.parseInt((String)v.getTag()));
				break;
			case R.id.keypad_plus_or_minus:
				toggleResult();
				break;
			case R.id.keypad_C:
				clearResult();
				break;
		}
	}
	
	private void clearResult()
	{
		result = 0;
		displayView.setText(result.toString());
	}
	
	private void toggleResult()
	{
		result = new Integer(-1*result);
		checkResult( result );
		displayView.setText(result.toString());
	}
	
	private void showResult(int number)
	{
		if( number == 0 && result == 0 )
		{
			return;
		}
		
		if( result == 0 )
		{
			displayView.setText(number+"");
		}
		else
		{
			displayView.append(number+"");
		}
		
		checkResult(Integer.parseInt(displayView.getText().toString()));
		displayView.setText(result.toString());
	}
	
	private void checkResult(int checkValue)
	{
		if(checkValue > max )
		{
			result = max;
		}
		else if(checkValue < min )
		{
			result = min;
		}
		else
		{
			result = checkValue;
		}
	}
	
	public int getResult()
	{
		return result;
	}
	
	public void setResult(int result)
	{
		this.result = result;
	}
	
	public int getMax()
	{
		return max;
	}
	
	public void setMax(int max)
	{
		this.max = max;
	}
	
	public int getMin()
	{
		return min;
	}
	
	public void setMin(int min)
	{
		this.min = min;
	}
}
